<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Order;

class DBInit extends Command
{
    public $_client = null;
    public $_model_order = null;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'db:init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'database table data update';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->_model_order = new Order();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while ($this->update_tp_spec_goods_price_danger_type()) {
            # code...
        }
    }

    public function update_tp_spec_goods_price_danger_type(){
        $query = array();
        $query['page'] = 1;
        $query['size'] = 100;
        $query['status'] = 1;
        $query['is_default'] = 1;
        $query['danger_type'] = false;
        $query['groupBy'] = 'goods_id';
        $rs = $this->_model_order->get_product_pirce_list($query);
        $rs = $rs->toarray();
        if($rs){
            $goods_id = array();
            foreach ($rs as $key => $value) {
                $goods_id[] = $value->goods_id;
            }
            $goods = $this->_model_order->get_product_goods_by_id($goods_id,array('goods_id','danger_type'));
            $goods = $goods->toarray();
            if($goods){
                foreach ($goods as $key => $value) {
                    if($value->danger_type){
                        $danger_type = $value->danger_type;
                    }else{
                        $danger_type = 0;
                    }
                    $this->_model_order->update_goods_price_danger_type($value->goods_id,array('danger_type' => $danger_type));
                }
            }else{
                $this->_model_order->update_goods_price_danger_type($goods_id,array('danger_type' => 0));
            }
            return true;
        }
        return false;
    }
}
